<template>
  <div class="Container">
    <el-transfer
      :titles="['待选属性', '已选属性']"
      filterable
      filter-placeholder=" "
      v-model="value"
      :data="data"
      @right-check-change="test"
    >
      <template #left-footer>
        <el-button type="primary" size="small" @click="dialogFormVisible = true">添加属性</el-button>
      </template>
      <template #right-footer>
        <el-button type="primary" :loading="flag" @click="GetSql">下一步</el-button>
      </template>
    </el-transfer>

    <el-dialog v-model="dialogFormVisible" title="添加属性值" width="500">
      <el-form :model="form">
        <el-form-item label="属性值" :label-width="formLabelWidth">
          <el-input
            v-model="form.name"
            autocomplete="off"
            placeholder="输入格式:属性值 "
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="Add">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted,inject } from 'vue';
import { useRouter } from 'vue-router';
import { mainStore } from '../store/index';
const $post = inject('$post');
const store = mainStore();
const fields = store.fields;
const router = useRouter();
const flag = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = '140px';
const data = reactive([]);
const value = ref([]);
const form = reactive({
  name: '',
});

const GetSql = () => {
  const selectedItems = data.filter(item => value.value.includes(item.key));
  const attributes = []
  selectedItems.forEach(se=>{
    attributes.push(se.label)
  })
  
  $post("/sql-helper/GeSql",{attributes}, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  }).then((res)=>{
      store.sql=res.data
      setTimeout(()=>{
        router.push('home')
      },2000)
      
      
  })
};

onMounted(() => {
  fields.forEach((line, index) => {
    data.push({
      key: index,
      label: line.value,
    });
  });
});

const Add = function () {
  data.push({
    key: 6666,
    label: form.name,
  });
  dialogFormVisible.value = false;
  form.name=''
};

function test() {
  
}
</script>

<style scoped>
.Container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.el-button .custom-loading .circular {
  margin-right: 6px;
  width: 18px;
  height: 18px;
  animation: loading-rotate 2s linear infinite;
}

.el-button .custom-loading .circular .path {
  animation: loading-dash 1.5s ease-in-out infinite;
  stroke-dasharray: 90, 150;
  stroke-dashoffset: 0;
  stroke-width: 2;
  stroke: var(--el-button-text-color);
  stroke-linecap: round;
}
</style>
